import "dart:math";

import "package:flutter/material.dart";

// 全局变量
class ScreenUnit {
  static late double screenWidth; //屏幕像素宽度
  static late double screenHeight; //屏幕像素高度
  static late double dpi; //像素比
  static late double statusHeight; //状态栏高度，小窗状态下可能为0
  static late double designWidth; //设计宽度
  static late double designHeight; //设计高度
  static late double scaleWidth; //物理像素宽度与设计缩放比
  static late double scaleHeight; //物理像素高度与设计缩放比

  ScreenUnit();
  ScreenUnit.init(
    BuildContext context, {
    double width = 750, //设计宽度
    double height = 1334, //设计高度
  }) {
    MediaQueryData mediaQuery = MediaQuery.of(context);
    screenWidth = mediaQuery.size.width;
    screenHeight = mediaQuery.size.height;
    dpi = max(mediaQuery.devicePixelRatio, 2);
    statusHeight = mediaQuery.padding.top;
    designWidth = width;
    designHeight = height;
    init();
  }

  // 初始化获取缩放比
  init() {
    double screenRealWidth = screenWidth * dpi; //屏幕物理宽度
    double screenRealHeight = screenHeight * dpi; //屏幕物理高度
    scaleWidth = screenRealWidth / designWidth;
    scaleHeight = screenRealHeight / designHeight;
  }

  // 修改屏幕宽度
  static setHeight(double height) {
    screenHeight = height;
  }

  // 修改屏幕高度
  static setWidth(double width) {
    screenWidth = width;
  }

  // 设置尺寸，是否适应宽度
  static setSp(num size, {bool dynamic = true}) {
    return dynamic ? scaleWidth * size : size;
  }
}
